using System.Collections;
using System.Collections.Generic;

namespace 数据挖掘实验
{
    public class ConfidenceCollector: IEnumerable<ConfidenceItem>
    {
        public Dictionary<int, ConfidenceItem> Confidences { get; } =
            new Dictionary<int, ConfidenceItem>();

        /// <summary>
        /// 使用键进行索引
        /// </summary>
        /// <param name="key">键</param>
        public ConfidenceItem this[int key]
        {
            get
            {
                if (Confidences.TryGetValue(key, out var item))
                    return item;
                return null;
            }
        }

        /// <summary>
        /// 收集置信度数据
        /// </summary>
        /// <param name="startPos">起始位置</param>
        /// <param name="items">从AprioriController到该AprioriItem节点的所有下标</param>
        /// <param name="aprioriCollector">Apriori采集器</param>
        /// <param name="transaction">所有事务</param>
        /// <param name="confidenceThreshold">置信度阈值(最小置信度)</param>
        public void Collect(int startPos, List<int> items,
            AprioriCollector aprioriCollector,
            List<string> transaction,
            float confidenceThreshold)
        {
            var key = items[startPos];  // 起始键
            var confidenceItem = new ConfidenceItem(key);
            int count = aprioriCollector[items].Count;
            if (confidenceItem.Compute(startPos + 1, aprioriCollector, count, items,transaction, confidenceThreshold)) // 计算如果满足最小置信度则进行添加
                Confidences.Add(key, confidenceItem);
        }

        public IEnumerator<ConfidenceItem> GetEnumerator()
        {
            foreach (var item in Confidences.Values)
            {
                yield return item;
                foreach (var childItem in item)
                {
                    yield return childItem;
                }
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}